<?php

namespace app\models;

use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;
use yii\data\Pagination;
use yii\data\ActiveDataProvider;


class CustSearch extends Cust
{

    public $region_name;
    public $user_name;


    public static function tableName()
    {
        return Cust::tableName();
    }

    public function rules()
    {
        return [
            [['region_id'], 'safe'],
            [['region_name'], 'safe'],
            [['cust_name'], 'safe'],
            [['user_name'], 'safe'],
            [['remark'], 'safe'],


        ];
    }
//    public function search($params, $prod_spec_id=null)
//    {
//
//    }

    /***
     * @name   searchByAccNbr  根据客户号码取出客户信息
     * @description
     * @param
     * @return 客户模型 包含客户属性
     * @param $tel
     */
    public function searchByAccNbr($accNbr)
    {
        $where  = [];
        $where['c.content'] = $accNbr;
        $where['c.attr_spec_id'] = 3;

        // 先取cust信息，如果有值取继续取最近的一条订单,获得这个号码对应的客户资料
        $query = Cust::find()->alias('a');
        $query->joinWith('custExternalAttrs  AS b');
        $query->joinWith('oneCustExternalAttrs  AS c');
        $query->where($where);
        $model = $query->one();
        return $model;
    }

    public function search($params, $user_id=null ,$wheres)
    {
        $where = [];
        $query = Cust::find()->alias('a');
        $query->joinWith('users  AS f');


        $query->joinWith('regions  AS b');
        $query->joinWith('parentRegions  AS c');

        if($wheres['parent_id']){

            $query->joinWith('orders  AS o1');
            $query->joinWith('orderResumess  AS od');

            $where['od.peculiarity'] = $wheres['parent_id'];

        }
        if ($wheres['state'])
        {
            $where['o.state'] = $wheres['state'];

        }

        if ($user_id)
        {
            $where['a.create_staff_id'] = $user_id;

        }

        if ($where)
        {
            $query->where($where);
        }




        //$query->joinWith('custExternalAttrs  AS e');

//        $where['a.prod_spec_id'] = $prod_spec_id;
//
//
//        if ($parent_id!=null)
//        {
//            $where['a.parent_id'] = $parent_id;
//        }
//
//
//        $query->where($where);


        $query->orderBy(['a.modify_date'=> SORT_DESC]);

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'pageSize' => 50,
            ],
        ]);


        $sort = $dataProvider->getSort(); // 获取yii自动生成的排序规则
        $sort->attributes['region_name'] = [ // 添加用户名的排序规则
            'asc' => ['b.region_name' => SORT_ASC],
            'desc' => ['b.region_name' => SORT_DESC],
        ];


        $dataProvider->setSort($sort);
//&& $this->validate()
        if (!($this->load($params) )) {
            return $dataProvider;
        }
        $query->andFilterWhere(['like', 'a.region_id', $this->region_id]);
        $query->andFilterWhere(['like', 'a.cust_name', $this->cust_name]);
        $query->andFilterWhere(['like', 'b.region_name', $this->region_name]);
        $query->andFilterWhere(['like', 'f.username', $this->user_name]);

        return $dataProvider;


    }


}

?>
